<?php

function redirect($to) {

  $schema = $_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http';

	$host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];



  if (headers_sent()) {

      echo '<script type="text/javascript">';

      echo 'window.location.href="'.$to.'";';

      echo '</script>';

      echo '<noscript>';

      echo '<meta http-equiv="refresh" content="0;url='.$to.'" />';

      echo '</noscript>';

  	} else {

      header("HTTP/1.1 301 Moved Permanently", false);

  	  header("Location: $schema://$host$to", false);

  	  exit();

  	}

}

function hasReportAccess($connection, $userId) {
    
    return query("SELECT count(1) as verslagen " .
            "FROM user_rights c " .
            "WHERE c.verslagen = 1 and c.user_id = ".$userId,
            $connection,
            "verslagen");
    
}

/** ZILVEREN TOREN FUNCTIES */

function isCaptain($connection, $userId) {
	
	return query("SELECT count(1) as captain " .
			"FROM zt_captain c join zt_tournament t " .
			"on t.tournament_id = c.tournament_id " .
			"WHERE t.isCurrent = 1 AND c.user_id = ".$userId,
			$connection,
			"captain");
	
}

function ztStarted($connection) {
    
    return query("SELECT COUNT(1) AS tournamentStarted FROM zt_group g JOIN zt_tournament zt ON zt.tournament_id = g.tournament_id WHERE zt.isCurrent = 1",
            $connection,
            "tournamentStarted");
    
}

function ztSubscriptionsEnded($connection) {
	
	return query("SELECT COUNT(1) as subscriptionEnded FROM zt_tournament t WHERE t.isCurrent = 1 AND SYSDATE() > t.subscriptionEndDate",
			$connection,
			"subscriptionEnded");
	
}

// convenience function om de huidige naam van het ZT tornooi te krijgen
function CURRENT_ZT_TOURNAMENT() {
	return queryNew("select tournament_name from zt_tournament where isCurrent = 1", "tournament_name");
}

/** LIGAKAMPIOENSCHAP FUNCTIES */



function CURRENT_NAME_INDIVIDUEEL_LIGAKAMPIOENSCHAP() {
	return queryNew("SELECT NAME FROM tournament ORDER BY subscription_end DESC LIMIT 1", "NAME");
}

/** HERBRUIKBARE FUNCTIES */

// convenience function to query 1 column on an existing connection
function query($sql, $connection, $columnName) {

    $result = mysql_query($sql, $connection);

    $row = mysql_fetch_assoc($result);
    
    return $row[$columnName];

}

// convenience function to query 1 column on a new connection
function queryNew($sql, $columnName) {
	
	$connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);

    mysql_select_db(DB_CATALOG);

    return query($sql, $connect, $columnName);
	
}

function isValidURL($url) {

    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);

}



function rejectTranIfError($query, $msg) {

  if ( mysql_errno() != 0 ) {

    $msg.= "</p> Tijdens het uitvoeren van volgende query: ";

    $msg.= "</p>".urlencode($query);

    $msg.= "</p> trad volgende fout op: ";

    $msg.= "</p>".urlencode(mysql_error());

    mysql_query("ROLLBACK");

    redirect("/error.php?msg=".$msg);

  }

}



function rejectIfError($query, $msg) {

  if (isset($query) && !empty($query)) {

    if ( mysql_errno() != 0 ) {

      $msg.= "</p> Tijdens het uitvoeren van volgende query: ";

      $msg.= "</p>".urlencode($query);

      $msg.= "</p> trad volgende fout op: ";

      $msg.= "</p>".urlencode(mysql_error());

      redirect("/error.php?msg=".$msg);

    }

  } else {

    redirect("/error.php?msg=".$msg);

  }

}



function show_error($error_code) {

    print ("<p>We hebben technische problemen, gelieve contact op te nemen met onze

    		<a href='webmaster@schaakliga.antwerpen.info'>webmaster</a>, wij lossen dit zo spoedig mogelijk op</p>");

    print($error_code);

   // mail("webmaster@schaakliga.antwerpen.info", "fout database", $error_code, "From: liga-daemon@schaakliga.antwerpen.info");

}



function array_addobj(&$array, $add) {

  $part = array_slice($array, 0, count($array));

  $addPart = array($add);

  $array = array_merge($part, $addPart);

}



function array_insertobj(&$array, $offset, $insert) {

  $firstPart = array_slice($array, 0, $offset);

  $secondPart = array_slice($array, $offset);

  $insertPart = array($insert);

  $array = array_merge($firstPart, $insertPart, $secondPart);

}



function array_deleteobj(&$array, $offset) {

  $firstPart = array_slice($array, 0, $offset);

  $secondPart = array_slice($array, $offset + 1);

  $array = array_merge($firstPart, $secondPart);

}



function array_switchobj(&$array, $offsetFrom, $offsetTo) {

  $firstPart = array_slice($array, 0, $offsetFrom);

  $secondPart = array_slice($array, $offsetTo + 1);

  $fromPart = array_slice($array, $offsetFrom, 1);

  $toPart = array_slice($array, $offsetTo, 1);

  $array = array_merge($firstPart, $toPart, $fromPart, $secondPart);

}



function getMatchPoints($bp, $dep) {

  if (strnatcasecmp("1", $dep) == 0) {

    if ($bp > 3) {

      return 2;

    } else if ($bp == 3) {

      return 1;

    } else {

      return 0;

    }

  } else {

    if ($bp > 2) {

      return 2;

    } else if ($bp == 2) {

      return 1;

    } else {

      return 0;

    }

  }

}



?>